/**
 * Created by wushanxin on 14-4-8.
 */

var log = require("../lib/log");
var config = require('../config');
var crypto = require('crypto');
var RUtil = require('./dataformatUtil');


var db = require("../lib/db");
var notify=require('../models/notify.js');

var virusers=require('../models/viruser.js');
exports.index = function(request, response) {

    var method = request.method || '';

    if(typeof(request.session.admin)=="undefined" || request.session.admin==null || request.session.admin.length==0)
    {
        response.send("<script>window.parent.location.href='login.html';</script>");
        return;
    }

    if(request.session.roleinfo.flag!==99)
    {
        response.send("<script>window.parent.location.href='index.html';</script>");
        return;
    }

    var notifydata=RUtil.notifydata(request);
    notifydata.retdata.nickname =request.session.admin.nickname;
    notifydata.retdata.roleinfo = request.session.roleinfo;
    notifydata.retdata.userinfo = request.session.admin;
    notifydata.retdata.rolelist={};
    notifydata.retdata.title="通知/告警管理";
    if(method.toUpperCase() === "GET") {

        virusers.selectForNotify(function(error,results) {
            if((error)!== null)
            {
                // 数据库异常
                notifydata.retdata.info = "载入失败：系统内部错误(错误码："+error.errno+")";
                notifydata.retdata.success=false;

                response.render("notifyinfo", notifydata);

            }
            else if(results.length <= 0) {
                // 不存在，直接响应页面
                notifydata.retdata.info = "没有任何代理商/商户记录!";
                notifydata.retdata.success=false;

                response.render("notifyinfo", notifydata);
            } else {
                //存在数据将数据发到页面
                notifydata.retdata.success=true;

                notifydata.retdata.viruser= results;

                if(request.query.oper == "add")
                {
                    notifydata.retdata.info = "添加通知/告警管理";
                    notifydata.retdata.success=true;

                    response.render("notifyinfo.html",notifydata);
                }
            }
        });

    }

    else if(method.toUpperCase() === "POST") {
        if(request.body.oper == "add")
        {
            var notifydata = RUtil.notify(request);
            notifydata.fromuserid=request.session.admin.id;

            var touseridandtonickname;
            if(Object.prototype.toString.call(request.body.touserid)!== '[object Array]')
            {
                touseridandtonickname=request.body.touserid;
                if(touseridandtonickname=="-1")
                {
                    notifydata.touserid="-1";
                    notifydata.tonickname="全体人员";
                }
                else
                {
                    notifydata.touserid=eval(touseridandtonickname.split('__'))[0];
                    notifydata.tonickname=eval(touseridandtonickname.split('__'))[1];
                }

            }
            else
            {
                var touserid=new Array();
                var tonickname=new Array();
                for(var ii = 0; ii < request.body.touserid.length;ii++)
                {
                    touserid[ii]=eval(request.body.touserid[ii].split('__'))[0];
                    tonickname[ii]=eval(request.body.touserid[ii].split('__'))[1];
                }
                notifydata.tonickname=tonickname.join(',');
                notifydata.touserid=touserid.join(',');
            }


            notifydata.msgcontent=request.body.msgcontent;
            notifydata.expiredate=request.body.expiredate;
            notifydata.level=request.body.level;

            notifydata.notifytype="管理员通知";
            notify.insert(notifydata, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    var info = { status:"error",info:'添加失败：系统内部错误(错误码：'+error.errno+')' };
                    if(error.errno==1062)
                        info = { status:"error",info:'添加失败：消息已经存在！)' };
                    response.send((info));
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    var info = { status:"error",info:'添加失败！' };
                    response.send( (info));
                } else {
                    //存在数据将数据发到页面
                    var info = { status:"success",info:'添加成功！' };
                    response.send( (info));
                }
            });
        }
    }

};


